package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntLevelInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntLevelInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户等级正式表操作接口实现类
 * Created by 赵奎伍 on 2020/3/25
 */
public class MchntLevelInfoMapperImpl implements MchntLevelInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public PageResult selectMchntLevelInfoByInstIdByPage(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLevelInfoByInstId");
        MchntLevelInfo mchntLevelInfo = new MchntLevelInfo();
        mchntLevelInfo.setInstId(instId);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntLevelInfo, MchntLevelInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取商户等级信息失败,instId:<{}>", instId);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<MchntLevelInfo> selectMchntLevelInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLevelInfoByInstId");
        MchntLevelInfo mchntLevelInfo = new MchntLevelInfo();
        mchntLevelInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntLevelInfo, MchntLevelInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户等级信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (MchntLevelInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectMchntLevelInfoByMchntLevelOrMchntLevelNameByPage(String instId, String mchntLevel, String mchntLevelName, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLevelInfoByMchntLevelOrMchntLevelName");
        MchntLevelInfo mchntLevelInfo = new MchntLevelInfo();
        mchntLevelInfo.setInstId(instId);
        if (null != mchntLevel && !mchntLevel.isEmpty()) {
            mchntLevelInfo.setMchntLevelCode(mchntLevel);
        }
        if (null != mchntLevelName && !mchntLevelName.isEmpty()) {
            mchntLevelInfo.setMchntLevelName(mchntLevelName);
        }
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntLevelInfo, MchntLevelInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据商户等级与商户等级名称模糊查询失败,instId:<{}>,mchntLevel:<{}>,mchntLevelName:<{}>", instId, mchntLevel, mchntLevelName);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<MchntLevelInfo> selectMchntLevelInfoByMchntLevelOrMchntLevelName(String instId, String mchntLevel, String mchntLevelName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLevelInfoByMchntLevelOrMchntLevelName");
        MchntLevelInfo mchntLevelInfo = new MchntLevelInfo();
        mchntLevelInfo.setInstId(instId);
        if (null != mchntLevel && !mchntLevel.isEmpty()) {
            mchntLevelInfo.setMchntLevelCode(mchntLevel);
        }
        if (null != mchntLevelName && !mchntLevelName.isEmpty()) {
            mchntLevelInfo.setMchntLevelName(mchntLevelName);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntLevelInfo, MchntLevelInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户等级与商户等级名称模糊查询失败,instId:<{}>,mchntLevel:<{}>,mchntLevelName:<{}>", instId, mchntLevel, mchntLevelName);
            return null;
        }
        return objectList.stream().map(item -> (MchntLevelInfo) item).collect(Collectors.toList());
    }

    @Override
    public MchntLevelInfo selectMchntLevelInfoByMchntLevelCode(String instId, String mchntLevelCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLevelInfoByMchntLevelCode");
        MchntLevelInfo mchntLevelInfo = new MchntLevelInfo();
        mchntLevelInfo.setInstId(instId);
        mchntLevelInfo.setMchntLevelCode(mchntLevelCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntLevelInfo, mchntLevelInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户等级信息失败,instId:<{}>,mchntLevelCode:<{}>,iReturnCode:<{}>", instId, mchntLevelCode, iReturnCode);
            return null;
        }
        return mchntLevelInfo;
    }
}
